package com.loadmore.oneday.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.loadmore.common.base.BaseFragment
import com.loadmore.common.constants.Api
import com.loadmore.common.model.BaseResponse
import com.loadmore.common.model.Plan
import com.loadmore.common.model.Task
import com.loadmore.common.net.Request
import com.loadmore.common.net.impl.JsonCallback
import com.loadmore.common.router.RouterPath
import com.loadmore.oneday.R
import com.loadmore.oneday.adapter.TaskListAdapter
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_plan_detail.*

/**
 *@author sunleilei
 *@date 2018/7/20
 *@time 10:35
 *
 */
@Route(path = RouterPath.PlanDetail)
class PlanDetailFragment :BaseFragment() {
    lateinit var taskListAdapter:TaskListAdapter
    override fun getLayoutId(): Int {
        return R.layout.fragment_plan_detail
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        actionBarTitle("任务")
        task_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        taskListAdapter = TaskListAdapter(context!!)
        task_list.adapter = taskListAdapter
        task_list.isNestedScrollingEnabled = false
        val data = arguments?.getSerializable("data") as Plan
        plan_name.text = data.title
        val plan_id = data.id.toString()
        getTasks(plan_id)
    }

    private fun getTasks(plan_id:String) {
        Request.get(Api.task, hashMapOf<String,Any>("plan_id" to plan_id),object :JsonCallback<BaseResponse<List<Task>>>(){
            override fun onSuccess(response: Response<BaseResponse<List<Task>>>?) {
                val data = response?.body()?.data
                if (data != null && !data.isEmpty()) {
                    taskListAdapter.append(data)
                }
            }

        })
    }

}